package com.ht.api.task;

import javax.sql.DataSource;

import com.ht.api.db.Db;
import com.ht.api.db.DbConfig;
import com.ht.api.db.Entity;
import com.ht.api.db.StreamLoadConfig;
import com.ht.api.db.task.TaskHandler;
import com.ht.api.db.util.HtUtil;
import com.ht.api.service.SopService;

import cn.hutool.json.JSONObject;

public class SopInitTask implements TaskHandler {

	@Override
	public String getName() {
		return "刷新旅程用户";
	}

    @Override
    public boolean handler(JSONObject requestParam, String groupId, long taskId) {

        long tenantId = requestParam.getLong("tenant_id");

        DataSource ds = DbConfig.tenantDSMap.get(tenantId);
        StreamLoadConfig config = DbConfig.useTenantSLC(tenantId);

        String sopId = requestParam.getStr("sop_id");

        Entity sop = Db.createSqlParser("sop").where("main_id", sopId).findOne(ds);

        SopService.refreshSopUser(ds, config, sop, true);

        sop.set("init_status", 2);
        Db.insert(ds, sop);

        HtUtil.createLog(ds, sopId, "初始化完成！");

        return true;
    }
}
